package com.younio.local.controller;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.younio.local.service.VirtualService;
import com.younio.local.util.Objects;
import com.younio.local.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lan  [1728209643@qq.com]
 * @create 2019-03-07 16:29
 * @desc
 */
@RestController
@RequestMapping("/virtual")
public class VirtualNumberController {

    @Autowired
    private VirtualService virtualService;

    @PostMapping("/get")
    public Result get(@RequestBody JSONObject object) {
        try {
            int n = object.getIntValue("n");
            Objects.checkArguments(n > 0, "待获取的虚拟TID数量必须大于零");
            String stationName = object.getString("stationName");
            Objects.checkArguments(Objects.strIsNotEmpty(stationName), "工位名称不能为空");
            List<Integer> lines = object.getJSONArray("lines").toJavaList(Integer.class);
            Objects.checkArguments(lines != null && !lines.isEmpty(), "线不能为空");
            return Result.OK(virtualService.get(n, stationName, lines));
        } catch (JSONException e) {
            throw new RuntimeException("参数错误");
        }
    }

    @PostMapping("/print")
    public Result print(@RequestBody JSONObject object) {
        try {
            String stationName = object.getString("stationName");
            Objects.checkArguments(Objects.strIsNotEmpty(stationName), "工位名称不能为空");
            List<Integer> lines = object.getJSONArray("lines").toJavaList(Integer.class);
            Objects.checkArguments(lines != null && !lines.isEmpty(), "线不能为空");
            List<String> codeList = object.getJSONArray("codeList").toJavaList(String.class);
            Objects.checkArguments(codeList != null && !codeList.isEmpty(), "待打印的虚拟TID不能为空");
            virtualService.createBartendPrint(stationName, lines, codeList);
        } catch (JSONException e) {
            throw new RuntimeException("参数错误");
        }
        return Result.OK();
    }
}
